<?php
require_once dirname(__FILE__)."/../init.php";

include "header.php";
include "navigation.php";

?>
<br/>
<?php
$success = $_GET['success'];
$operation = $_GET['operation'];

$operationDone = null;

if ($operation == "add") {
    $operationDone = "saved";
} else if ($operation == "delete") {
    $operationDone = "deleted";
}

if ($success != null && $success == "true") {
?>
<fb:success message="Contact has been <?php echo $operationDone ?>." />
<?php
}
?>
<style>
    .contactForm {float: left; width: 45%;}
    .contactFormHeader {
        padding: 10px 10px;
        background-color: #eaeaea;
        font-weight: bold;
        margin-top: 5px;
        border: 1px solid #ddd;
        border-top: 1px solid #aaa;

    }
    .contactList {float: left; padding-left: 20px; width: 50%}
</style>
<div class="contactForm">
    <div class="contactFormHeader">Add new contact</div>
    <fb:editor action="index.php?_action=addContact" width="250" labelwidth="100">
        <fb:editor-custom label="Full Name">
                <fb:friend-selector uid="<?=user?>" name="selectedFriend" idname="uid" />
        </fb:editor-custom>
        <fb:editor-text name="mobileNumber" maxlength="13" label="Mobile number"/>
        <fb:editor-custom>
                E.g. +639271234567
        </fb:editor-custom>
        <fb:editor-buttonset>
            <fb:editor-button value="Save"/>
        </fb:editor-buttonset>
    </fb:editor>

</div>
<div class="contactList">
    <?php
    $owner = ServiceFactory::getUserService()->getByUid($_uid);
    $contactList = ServiceFactory::getContactService()->getContactsByUserId($owner->getId());

    ?>
    <style>
        .hDivider { height: 1px; width: 100%; border-bottom: 1px solid #ddd; clear: both; padding-top: 5px;}
        .contactItemBlock {clear: both; padding-top: 5px; position: relative;}
        .contactItemInfo {float: left; padding-left: 5px; padding-bottom: 5px; }
        .contactItemFullName {clear: both; }
        .contactItemMobileNumber {color: #999; clear: both;}
        .contactItemPhoto {min-height: 25px; max-height: 50px; width: 60px; float: left; padding-left: 10px; border-left: 1px solid #ddd; }
        .contactItemOptions {float: left; text-align: right; padding-right: 10px; }
    </style>
    <?php
    if (empty($contactList)) {
    ?>
    <div style="text-align: center; color: #999;"> You have not added any contacts yet. </div>
    <?php
    }
    foreach ($contactList as $contactItem) {
    ?>
    <div class="contactItemBlock">
        <div class="contactItemOptions">
            <a href="#">Edit</a><br/>
            <a href="#" clicktoshowdialog="delete_dialog_<?php echo $contactItem->getId(); ?>">Delete</a>
        </div>
        <div class="contactItemPhoto">
            <?php
            $contactUid = $contactItem->getUid();
            if (!empty($contactUid)) {
            ?>
            <fb:profile-pic uid="<?php echo $contactItem->getUid() ?>" size="square"/>
            <?php 
            }
            ?>
            &nbsp;
        </div>
        <div class="contactItemInfo">
            <div class="contactItemFullName"><?php echo $contactItem->getFullName(); ?></div>
            <div class="contactItemMobileNumber"><?php echo $contactItem->getMobileNumber(); ?></div>
        </div>
        <div class="hDivider">&nbsp;</div>
    </div>
    <?php } ?>

    <?php
    foreach ($contactList as $contactItem) {
    ?>

    <fb:dialog id="delete_dialog_<?php echo $contactItem->getId(); ?>" cancel_button="1">
      <fb:dialog-title>Delete contact</fb:dialog-title>
      <fb:dialog-content>Are you sure you want to delete <?php echo $contactItem->getFullName(); ?>?
          <form method="post" name="delete_form_<?php echo $contactItem->getId(); ?>" id="delete_form_<?php echo $contactItem->getId(); ?>" action="index.php?_action=deleteContact">
          <input type="hidden" name="contactId" value="<?php echo $contactItem->getId(); ?>">
      </form>
      </fb:dialog-content>
      <fb:dialog-button type="submit" value="Yes" form_id="delete_form_<?php echo $contactItem->getId(); ?>"/>
    </fb:dialog>

    <?php } ?>
</div>